package com.jic.member.impl;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.orm.Pagination;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.mapper.TripBranchEntityMapper;
import com.jic.member.mapper.TripBranchNodeEntityMapper;
import com.jic.member.mapper.TripEventInfoEntityMapper;
import com.jic.member.mapper.TripExecuteInfoEntityMapper;
import com.jic.member.entity.TripBranchEntity;
import com.jic.member.entity.TripBranchNodeEntity;
import com.jic.member.entity.TripEventInfoEntity;
import com.jic.member.entity.TripExecuteInfoEntity;
import com.jic.member.service.TripEventInfoService;
import com.jic.market.api.manage.CouponManangeApi;
import com.jic.market.request.rest.coupon.SendCouponRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分支节点事件触发记录表
 * @author :
 * @email :
 * @since : 2020-06-08 11:03:19
 * @version : v1.0.0
 */
@Service("tripEventInfoService")
public class TripEventInfoServiceImpl implements TripEventInfoService, Serializable {

    @Autowired
    private TripEventInfoEntityMapper tripEventInfoEntityMapper;

    @Resource
    private CouponManangeApi couponManangeApi;

    @Resource
    private TripBranchNodeEntityMapper tripBranchNodeEntityMapper;
    @Resource
    private TripBranchEntityMapper tripBranchEntityMapper;

    @Resource
    private TripExecuteInfoEntityMapper tripExecuteInfoEntityMapper;

    @Override
    public long insert(TripEventInfoEntity tripEventInfoEntity) {
        long flag = tripEventInfoEntityMapper.insert(tripEventInfoEntity);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return tripEventInfoEntityMapper.delete(id);
    }

    @Override
    public long update(TripEventInfoEntity tripEventInfoEntity) {
        return tripEventInfoEntityMapper.update(tripEventInfoEntity);
    }

    @Override
    public TripEventInfoEntity queryById(Long id){
        return tripEventInfoEntityMapper.queryById(id);
    }

    @Override
    public List<TripEventInfoEntity> findAll(TripEventInfoEntity tripEventInfoEntity){
        return tripEventInfoEntityMapper.findAll(tripEventInfoEntity);
    }

    @Override
    public PageResult<TripEventInfoEntity> queryPage(TripEventInfoEntity tripEventInfoEntity, Page pageQuery) {
        Pagination pagination = Pagination.build(pageQuery.getStart(), pageQuery.getLimit());
        List<TripEventInfoEntity> list = tripEventInfoEntityMapper.findOfPage(tripEventInfoEntity,pagination);
        long count = tripEventInfoEntityMapper.countOfPage(tripEventInfoEntity);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public Boolean sendCoupon(Long tripId, Long branchNodeId, List<String> thirdUserIdList) {
        //查询旅程id 分支id 模板code
        TripBranchNodeEntity tripBranchNodeEntity = tripBranchNodeEntityMapper.queryById(branchNodeId);
        String nodeVariable = tripBranchNodeEntity.getNodeVariable();
        Map<String,Object> nodeVariableMap = JSONArray.parseObject(nodeVariable, Map.class);
        TripBranchEntity tripBranchEntity = tripBranchEntityMapper.queryById(tripBranchNodeEntity.getBranchId());
        SendCouponRequest sendCouponRequest = new SendCouponRequest() ;
        sendCouponRequest.setTripId(tripBranchEntity.getTripId());
        sendCouponRequest.setThirdUserIdList(thirdUserIdList);
        sendCouponRequest.setBranchId(tripBranchNodeEntity.getBranchId());
        sendCouponRequest.setBranchNodeId(tripBranchNodeEntity.getId());
        sendCouponRequest.setNodeTemplateCode(tripBranchNodeEntity.getNodeTemplateCode());
        sendCouponRequest.setPromotionId(nodeVariableMap.get("promotionId").toString());
        RestResult<Boolean> booleanRestResult = couponManangeApi.sendCoupon(sendCouponRequest);

        thirdUserIdList.forEach(thirdUserId->{
            tripExecuteInfoEntityMapper.insert(new TripExecuteInfoEntity(){{
                setThirdUserId(thirdUserId);
                setBranchId(tripBranchNodeEntity.getBranchId());
                setTripId(tripBranchEntity.getTripId());
                setBranchNodeId(branchNodeId);
                setNodeTemplateCode(tripBranchNodeEntity.getNodeTemplateCode());
                setStatus(1);
                setType(1);
                setCreateTime(new Date());
                setUpdateTime(new Date());
            }});
        });
        return booleanRestResult.getData();
    }
}
